package com.gykfbxxh.imovie.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.gykfbxxh.imovie.R;
import com.gykfbxxh.imovie.base.AppBaseAdapter;
import com.gykfbxxh.imovie.bean.film.FilmEntity;
import com.gykfbxxh.imovie.utils.glide.ImageLoader;

import java.util.List;

/**
 * Created by xuxinghai
 * On 2017/9/12
 * 想看grid布局
 */

public class MineGridAdapter extends AppBaseAdapter<FilmEntity> {
    private View.OnClickListener onClickListener;

    public MineGridAdapter(List<FilmEntity> list, Context context, View.OnClickListener onClickListener) {
        super(list, context);
        this.onClickListener = onClickListener ;
    }

    @Override
    public View creatView(int position, View convertView, ViewGroup parent) {
        FilmEntity filmEntity = list.get(position);
        ViewHolder holder = null ;
        ViewEmptyHolder eHolder = null ;
        int type = getItemViewType(position);
        if(convertView==null) {
            if(type == 1) {
                convertView = inflater.inflate(R.layout.item_grid_like_empty,null);
                eHolder = new ViewEmptyHolder();
                eHolder.iv_imageview_empty = (ImageView) convertView.findViewById(R.id.iv_imageview_empty);
                convertView.setTag(eHolder);
            }else {
                convertView = inflater.inflate(R.layout.item_grid_like,null);
                holder = new ViewHolder();
                holder.iv_imageview = (ImageView) convertView.findViewById(R.id.iv_imageview);
                holder.iv_right_top = (ImageView) convertView.findViewById(R.id.iv_right_top);
                convertView.setTag(holder);
            }
        }else {
            if(type==1) {
                eHolder = (ViewEmptyHolder) convertView.getTag();
            }else {
                holder = (ViewHolder) convertView.getTag();
            }
        }

        if(type==1) {
            eHolder.iv_imageview_empty.setImageResource(R.drawable.g_accretion);
            eHolder.iv_imageview_empty.setOnClickListener(onClickListener);
        }else {
            holder.iv_imageview.setOnClickListener(onClickListener);
            holder.iv_imageview.setTag(R.id.tag_first,filmEntity);
            ImageLoader.getInstance().listLoadImage2Cache(context,holder.iv_imageview,filmEntity.getFPhoto());
            holder.iv_right_top.setVisibility(View.VISIBLE);
            holder.iv_right_top.setOnClickListener(onClickListener);
            holder.iv_right_top.setTag(R.id.tag_first,filmEntity);
            holder.iv_right_top.setTag(R.id.tag_secong,position);
        }

        return convertView;
    }
    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        if(!TextUtils.isEmpty(list.get(position).getIsEmpty())) {
            return 1;
        }else {
            return 0;
        }

    }

    class ViewHolder{
        ImageView iv_imageview;
        ImageView iv_right_top;
    }
    class ViewEmptyHolder{
        ImageView iv_imageview_empty;
    }
}
